package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author gq
 * @since 2022-06-21
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {
    /**
     * 保存作者
     * @param apAuthor
     * @return
     */
    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor apAuthor) {
//        判空
        if (apAuthor == null || apAuthor.getUserId() == null || apAuthor.getWmUserId() == null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        根据user_id判断是否存在记录
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApAuthor::getUserId,apAuthor.getUserId());
        ApAuthor one = this.getOne(queryWrapper);
//        如果存在作者，直接返回
        if (one != null){
            return ResponseResult.okResult(one);
        }
//        如果不存在，新增作者信息
        apAuthor.setCreatedTime(new Date());
        this.save(apAuthor);
        return ResponseResult.okResult(apAuthor);
    }
}
